package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.SystemMessage;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ISystemMessageService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ISystemMessageService systemMessageService;
    /**
     * 待发布游记列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String user(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        //待发布
        qo.setState(Travel.STATE_WAIT);
        //公开
        qo.setIsPublic(true);
        //按照创建时间正序
        qo.setOrderBy("t.createTime");
        model.addAttribute("pageInfo",travelService.queryForList(qo));
        return "travel/list";
    }

    @RequestMapping("getContentById")
    @ResponseBody
    public TravelContent getContentById(Long id){
        return travelService.getContentById(id);
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Travel travel){
        travelService.changeState(travel);
        //判断是 发布还是取消然后 发送系统信息
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setUserId(travel.getUserId());
        systemMessage.setCreateTime(new Date());
        systemMessage.setState(0);
        //发布
        if(Travel.STATE_RELEASE==travel.getState()){

            systemMessage.setContent("  恭喜亲，你所发布的游记\""+"<a class='unreadHref' href='/travelContent.html?id="+travel.getId()+"'>"+travel.getTitle()+"</a>"+"\"，已经成功审核通过！" );
        }else if(Travel.STATE_REJECT==travel.getState()){
            systemMessage.setContent("  坏消息，亲你所发布的游记\""+"<a class='unreadHref' href='/mine/editTravels.html?id="+travel.getId()+"'>"+travel.getTitle()+"</a>"+"\"，由于内容和我司所规定的规则，有所出入，请修改后再发布！" );
        }else if(Travel.STATE_WAIT==travel.getState()){
            systemMessage.setContent("  坏消息，亲你已经所发布的游记\""+"<a class='unreadHref' href='/mine/editTravels.html?id="+travel.getId()+"'>"+travel.getTitle()+"</a>"+"\"，由于内容和我司所规定的规则，有所出入，已经被下架！" );
        }
        systemMessageService.insert(systemMessage);
        return new JsonResult();
    }
    //查询所有的发布后的游记
    @RequestMapping("/selectRelease")
    @ResponseBody
    public Object selectRelease(){
        return travelService.selectRelease();
    }
}
